package com.atgion.manager.service.impl;


import com.atgion.manager.mapper.ImageMapper;
import com.atgion.manager.service.FileUploadService;
import com.atgion.manager.service.ImageService;
import com.atgion.spzx.model.entity.ImageManagement.Images;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Images> implements ImageService {

    @Autowired
    private FileUploadService fileUploadService;

    @Override
    public Page<Images> findByPage(Integer page, Integer limit, Integer fileName,Integer isVideo) {
        Page<Images> imagePage = Page.of(page, limit);
        Page<Images> page1 = lambdaQuery()
                .eq(fileName != null && fileName != -1, Images::getCategoryId, fileName)
                .eq(isVideo != null, Images::getIsVideo, isVideo)
                .page(imagePage);
        return page1;
    }

    @Override
    public void UpdateImageById(Integer ImageById,Integer id) {
        lambdaUpdate()
                .set(Images::getCategoryId, id)
                .set(Images::getUpdateTime, new Date())
                .eq(Images::getId,ImageById)
                .update();
    }

    @Override
    public void removeByids(List<Integer> id) {
        for (Integer i : id) {
            Images images = lambdaQuery().eq(Images::getId, i).one();
            System.out.println(images.getFilePath());
            fileUploadService.deleteFile(images.getFilePath());
        }
        removeByIds(id);
    }
}
